package com.ruoyi.equ.service.impl;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.equ.domain.EquAsset;
import com.ruoyi.equ.domain.vo.EquStockinVos;
import com.ruoyi.equ.domain.vo.StockinAndAssetVo;
import com.ruoyi.equ.mapper.EquAssetMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.equ.mapper.EquStockinMapper;
import com.ruoyi.equ.domain.EquStockin;
import com.ruoyi.equ.service.IEquStockinService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 验收入库Service业务层处理
 *
 * @author ruoyi
 * @date 2025-11-05
 */
@Service
public class EquStockinServiceImpl implements IEquStockinService {
    @Autowired
    private EquStockinMapper equStockinMapper;
    @Autowired
    private EquAssetMapper equAssetMapper;

    /**
     * 查询验收入库
     *
     * @param stockId 验收入库主键
     * @return 验收入库
     */
    @Override
    public EquStockin selectEquStockinByStockId(Long stockId) {
        return equStockinMapper.selectEquStockinByStockId(stockId);
    }

    /**
     * 查询验收入库列表
     *
     * @param equStockin 验收入库
     * @return 验收入库
     */
    @Override
    public List<EquStockin> selectEquStockinList(EquStockin equStockin) {
        return equStockinMapper.selectEquStockinList(equStockin);
    }

    /**
     * 新增验收入库
     *
     * @param equStockin 验收入库
     * @return 结果
     */
    @Override
    public int insertEquStockin(EquStockin equStockin) {
        equStockin.setCreateTime(DateUtils.getNowDate());
        return equStockinMapper.insertEquStockin(equStockin);
    }

    /**
     * 修改验收入库
     *
     * @param equStockin 验收入库
     * @return 结果
     */
    @Override
    public int updateEquStockin(EquStockin equStockin) {
        equStockin.setUpdateTime(DateUtils.getNowDate());
        return equStockinMapper.updateEquStockin(equStockin);
    }

    /**
     * 批量删除验收入库
     *
     * @param stockIds 需要删除的验收入库主键
     * @return 结果
     */
    @Override
    public int deleteEquStockinByStockIds(Long[] stockIds) {
        return equStockinMapper.deleteEquStockinByStockIds(stockIds);
    }

    /**
     * 删除验收入库信息
     *
     * @param stockId 验收入库主键
     * @return 结果
     */
    @Override
    public int deleteEquStockinByStockId(Long stockId) {
        return equStockinMapper.deleteEquStockinByStockId(stockId);
    }

    /**
     * 入库生成台账记录及新增
     */
    @Override
    @Transactional
    public Boolean finishStockinAndAsset(StockinAndAssetVo stockinAndAssetVo) {
        //从前端拿到这些数据
        //设备的入库台账信息
        EquStockin equStockin = stockinAndAssetVo.getEquStockin();
        //设备本身的信息
        EquAsset equAsset = stockinAndAssetVo.getEquAsset();
        //新增这两条数据到数据库
        try {
            //新增入库单
            int flagStockin = equStockinMapper.insertEquStockin(equStockin);
            //入库
            int flagAsset = equAssetMapper.insertEquAsset(equAsset);
            return flagStockin > 0 && flagAsset > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    /**
     * 查询验收入库列表
     */
    @Override
    public List<EquStockinVos> selectEquStockinVoList(EquStockinVos equStockinVos) {
        return equStockinMapper.selectEquStockinVoList(equStockinVos);
    }
    /**
     * 查询验收入库列表
     */
    @Override
    public List<EquStockin> getStockinsByAssetId(Long assetId) {
        return equStockinMapper.getStockinsByAssetId(assetId);
    }

}
